package com.sunday.volunteer.ui.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;
import com.sunday.volunteer.BaseApp;
import com.sunday.volunteer.R;
import com.sunday.volunteer.entity.Address;
import com.sunday.volunteer.http.HttpClient;
import com.sunday.volunteer.utils.BaseUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Pual's PC on 2016/10/17.
 */
public class AddAddressActivity extends BaseActivity {

    @Bind(R.id.title_text)
    TextView title_text;
    @Bind(R.id.title_right_text)
    TextView title_right_text;
    @Bind(R.id.name)
    EditText name;
    @Bind(R.id.mobile)
    EditText mobile;
    @Bind(R.id.address)
    EditText address;

    private long addressId = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);
        ButterKnife.bind(this);


        title_right_text.setText("确认");


        if(getIntent().getExtras() != null){
            Address item = (Address) getIntent().getSerializableExtra(BaseUtil.ITEM);
            title_text.setText("修改地址");
            name.setText(item.getReceiveName());
            mobile.setText(item.getReceiveMobile());
            address.setText(item.getReceiveAddress());
            addressId = item.getId();
        }else {
            title_text.setText("添加地址");
        }
    }

    @OnClick(R.id.title_right_text)
    void saveAddress(){
        showLoadingDialog(R.string.save);
        if(TextUtils.isEmpty(name.getText().toString())){
            ToastUtils.showToast(mContext , "请输入姓名");
            return;
        }
        if(!StringUtils.isMobileNO(mobile.getText().toString())){
            ToastUtils.showToast(mContext , "手机号码不合法");
            return;
        }
        if(TextUtils.isEmpty(address.getText().toString())){
            ToastUtils.showToast(mContext , "请输入详细地址");
            return;
        }

        Call<ResultDO> call = HttpClient.getHttpAdapter().save(BaseApp.getInstance().getMemberId() ,name.getText().toString() ,mobile.getText().toString() ,address.getText().toString() ,addressId);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if(response.body() == null || isFinish){
                    return;
                }
                if(response.body().getCode() == 0){
                    finish();
                }else {
                    ToastUtils.showToast(mContext , response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
            }
        });

    }

}
